<template>
  <div>
    <!--    搜索表单-->
    <div>
      <i class="el-icon-back" @click="$router.push('/manage/user')"></i>
    </div>
    <div>
      <el-input style="width: 120px" placeholder="请输入图书名" v-model="params.bookName"></el-input>
      <el-input style="width: 120px; " placeholder="请输入借阅人" v-model="params.userName"></el-input>
      <el-input style="width: 120px; " placeholder="请输入书号" v-model="params.bookIsbn"></el-input>
      <el-button style="margin-left: 20%; margin-top: 5px" type="primary" @click="load()">
        <i class="el-icon-search"></i> 搜索
      </el-button>
      <el-button style="margin-left: 15%" type="danger" @click="reset">
        <i class="el-icon-refresh"></i>重置
      </el-button>
    </div>
    <!--增删改查-->
    <div style="margin: 10px 0">
    </div>
    <!--表单内容-->
    <el-table :data="tableData" stripe row-key="id" default-expand-all>
      <el-table-column prop="bookName" label="书名"></el-table-column>
      <el-table-column prop="cover" label="封面" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.cover" :preview-src-list="[scope.row.cover]"></el-image>
        </template>
      </el-table-column>

      <!--操作-->
      <el-table-column
          fixed="right"
          label="操作"
          width="190">

        <template v-slot="scope">
          <el-button @click="handleEdit(scope.row)" type="success" size="small">查看<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              title="您确定删除这行信息吗？"
              @confirm="del(scope.row.id)"
          >

            <el-button slot="reference" type="danger" size="small">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>

        </template>
      </el-table-column>


    </el-table>

<!--    //查看-->
    <el-dialog title="用户信息" :visible.sync="updateTab" width="90vw" style="font-weight: bolder">
      <el-form label-width="80px" style="" :model="updateForm" :rules="rules" ref="rulerForm">

        <el-form-item label="书名" prop="bookName">
          <el-input v-model="updateForm.bookName" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="书号" prop="bookIsbn">
          <el-input v-model="updateForm.bookIsbn" auto-complete="off"  disabled></el-input>
        </el-form-item>

        <el-form-item label="借出时间" prop="createTime">
          <el-input v-model="updateForm.createTime" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="借出状态" prop="status">
          <el-input v-model="updateForm.status" auto-complete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="借出天数" prop="days">
          <el-input v-model="updateForm.days" auto-complete="off" disabled ></el-input>
        </el-form-item>
        <el-form-item label="还书日期" prop="returnTime">
          <el-input v-model="updateForm.returnTime" auto-complete="off"  disabled></el-input>
        </el-form-item>
        <el-form-item label="所需书币" prop="score">
          <el-input v-model="updateForm.score" auto-complete="off" disabled></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelUpdate">取 消</el-button>
      </div>
    </el-dialog>


    <!--    分页-->
    <div style="text-align: center; ">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>


  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";
import user from "@/views/user/User";

export default {
  name: "returnBook",
  data() {

    return {
      books: [],
      updateTab: false,
      user: Cookies.get('user') ? JSON.parse(Cookies.get('user')) : {},
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 5,
      },
      form: {},
      updateForm: {},
    }

  },
  created() {
    this.load();
    request.get('/user/' + this.user.id).then(res => {
      this.user = res.data;
    })
  },


  methods: {

    handleCancelUpdate() {
      this.updateTab = false
      this.load()
    },

    load() {
      this.params.userId = this.user.id
      request.get('/returnBook/page', {
        params: this.params,
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },

    handleEdit(row) {
      this.updateForm = row;
      this.updateTab = true;
    },


    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        bookName: '',
        bookIsbn: '',
        userName: '',

      }
      this.load()
    },

    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },


    del(id) {
      request.delete('/returnBook/delete/' + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  }
}
</script>

<style scoped>
.el-input /deep/ .el-input__inner{
  color: rgb(83,80,80) !important;
  cursor: pointer;

}
</style>